package com.example.springinitialization.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springinitialization.exception.MyException;
import com.example.springinitialization.mapper.UserMapper;
import com.example.springinitialization.pojo.dto.userDto.*;
import com.example.springinitialization.pojo.response.user.UserResponse;
import com.example.springinitialization.pojo.vo.User;
import com.example.springinitialization.service.UserService;

import com.example.springinitialization.utils.DeleteDto;
import com.example.springinitialization.utils.StringUtil;
import com.example.springinitialization.utils.token.LoginUser;
import com.example.springinitialization.utils.token.LoginUserUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author centos7
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-08-08 00:28:29
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {


    @Override
    public List<UserResponse> getUserList() {
        // 获取学生列表
        List<User> list = list();
        List<UserResponse> userResponseList = new ArrayList<>();
        for(User user: list) {
            UserResponse userResponse = BeanUtil.copyProperties(user, UserResponse.class);
            userResponseList.add(userResponse);
        }
        return userResponseList;
    }

    @Override
    public List<UserResponse> getUserListByParams(UserQueryDto userQueryDto) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda()
                .eq(userQueryDto.getUsername() != null && "".equals(userQueryDto.getUsername()), User::getUsername, userQueryDto.getUsername())
                .eq(userQueryDto.getUserAccount() != null && "".equals(userQueryDto.getUserAccount()), User::getUserAccount, userQueryDto.getUserAccount())
                .eq(userQueryDto.getSex() != null && "".equals(userQueryDto.getSex()), User::getSex, userQueryDto.getSex())
                .eq(userQueryDto.getBirthday() != null, User::getBirthday, userQueryDto.getBirthday())
                .eq(userQueryDto.getAddress() != null && "".equals(userQueryDto.getAddress()), User::getAddress, userQueryDto.getAddress());
        List<User> userList = list(userQueryWrapper);
        List<UserResponse> userResponseList = new ArrayList<>();
        for(User user: userList) {
            UserResponse userResponse = BeanUtil.copyProperties(user, UserResponse.class);
            userResponseList.add(userResponse);
        }
        return userResponseList;
    }

    @Override
    public UserResponse getUserById(Long id) {
        User user = getById(id);
        return BeanUtil.copyProperties(user, UserResponse.class);
    }

    @Override
    public void AddUser(UserAddDto userAddDto) {
        User user = BeanUtil.copyProperties(userAddDto, User.class);
        saveOrUpdate(user);
    }

    @Override
    public void UpdateUser(UserUpdateDto userUpdateDto) {
        User user = BeanUtil.copyProperties(userUpdateDto, User.class);
        saveOrUpdate(user);
    }

    @Override
    public void delUser(DeleteDto deleteDto) {
        removeById(deleteDto.getId());
    }

    @Override
    public boolean resister(UserResisterDto userResisterDto) {
        User user = new User();
        // 随机生成六位不重复数字作为用户账号
        List<User> list = null;
        String account;
        // 校验直到符合条件的账号出现
        do {
            account = String.valueOf(StringUtil.getRandomNCount(6));
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.lambda().eq(User::getUserAccount, account);
            list = this.list(userQueryWrapper);
        } while(list == null);

        String password = userResisterDto.getUserPassword();
        user.setUserAccount(account);
        if(!Objects.equals(password, userResisterDto.getRePassword())) {
            throw new MyException("两次输入的密码不一致");
        }
        BeanUtil.copyProperties(userResisterDto, user);
        // 密码转换为md5进行存储
        user.setUserPassword(StringUtil.transferStrToMD5(password));
        return this.save(user);
    }

    @Override
    public LoginUser login(UserLoginDto userLoginDto) {
        // TODO 验证码校验逻辑
        String checkCode = userLoginDto.getCheckCode();
        if(!checkCode.equals("")) {
            throw new MyException("验证码错误");
        }
        // 转换后的密码
        String password = StringUtil.transferStrToMD5(userLoginDto.getUserPassword());
        String userAccount = userLoginDto.getUserAccount();
        // 查询是否有用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getUserAccount, userAccount).eq(User::getUserPassword, password);
        List<User> userList = this.list(userQueryWrapper);
        if(userList == null || userList.size() == 0) {
            throw new MyException("用户名或者密码错误");
        }
        User user = userList.get(0);
        String token = IdUtil.simpleUUID();
        return new LoginUser(user, token, System.currentTimeMillis());
    }

    @Override
    public boolean logout() {
        LoginUserUtils.removeUser();
        return true;
    }
}